﻿using System;
using System.Linq;
using KeyWithTypeId.Model;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
using Xtensive.Orm.Providers;

namespace KeyWithTypeId
{
    public class Program
    {
        public static void Main()
        {
            // Loading domain configuration section with name "Default".
            var domainConfiguration = DomainConfiguration.Load("Default");
            var domain = Domain.Build(domainConfiguration);

            Console.WriteLine("This functionality is broken in latest releases. Quitting...");
            return;

            // Shaun of the Dead movie
            using (var session = domain.OpenSession()) {
                using (var t = session.OpenTransaction()) {

                    var ed = new Zombie(session) {
                        Name = "Ed",
                        CanWalk = true
                    };

                    var shaun = new Person(session) {
                        Name = "Shaun",
                        UndeadFriend = ed
                    };

                    t.Complete();
                }
            }

            using (var session = domain.OpenSession()) {
                // Subscribing to Session event to display actual DbCommands
                session.Events.DbCommandExecuted += (sender, args) => Console.WriteLine(args.Command.ToHumanReadableString());

                using (var t = session.OpenTransaction()) {
                    Console.WriteLine("Loading Person instance");
                    var person = session.Query.All<Person>().First();

                    Console.WriteLine("\nObtaining 'Person.UndeadFriend' instance. As TypeId is known from the reference field, no additional query to find out the exact type is required");
                    var undead = person.UndeadFriend;
                    Console.WriteLine(undead.ToString());

                    t.Complete();
                }
            }

        }
    }
}
